#include "GraphicsTextItem.h"
#include <QFontMetrics>
#include <QPainter>

GraphicsTextItem::GraphicsTextItem(GraphicsBaseItem* parent)
	:m_parent(parent)
{
	assert(m_parent);
	connect(parent, &QGraphicsObject::xChanged, this, &GraphicsTextItem::onRefreshPostion);
	connect(parent, &QGraphicsObject::yChanged, this, &GraphicsTextItem::onRefreshPostion);
	connect(&parent->getItemData(), &GraphicsBaseItemData::sigRotateAngleChanged, this, &GraphicsTextItem::onRefreshPostion);
	connect(&parent->getItemData(), &GraphicsBaseItemData::sigSizeChanged, this, &GraphicsTextItem::onRefreshPostion);
	setZValue(100);
	hide();
}

void GraphicsTextItem::setText(const QString& text)
{
	m_text = text;
	// 计算字体的大小
	QFont font = getFont();
	QFontMetrics fontMeteric(font);
	QRect rect = fontMeteric.boundingRect(0, 0, 200, 800, Qt::TextSingleLine, text);
	rect.adjust(-8, -4, 8, 4);
	// 根据文字设置尺寸
	setRect(rect);
}

void GraphicsTextItem::onRefreshPostion()
{
	GraphicsBaseItem* grandFatherItem = m_parent->getItemData().getParentItem();
	assert(grandFatherItem);
	const QSizeF scaleSize = m_parent->getItemData().getScaleSize();
	if (scaleSize.width() <= 0 || scaleSize.height() <= 0)
		return;

	const QRectF grandFatherRect = grandFatherItem->getItemData().getScaleRect();
	QRectF parentRect(-scaleSize.width() / 2, -scaleSize.height() / 2, scaleSize.width(), scaleSize.height());
	parentRect.adjust(80, 80, -80, -80);

	const QPolygonF scenePolygon = m_parent->mapToScene(parentRect);
	// 检查右下，右上，左下，左上显示点是否在抠图区域内
	if (grandFatherRect.contains(scenePolygon.at(2))) {
		setPos(scenePolygon.at(2));
		m_positionType = BottomRight_Enum;
	}
	else if (grandFatherRect.contains(scenePolygon.at(1))) {
		setPos(scenePolygon.at(1));
		m_positionType = TopRight_Enum;
	}
	else if (grandFatherRect.contains(scenePolygon.at(3))) {
		setPos(scenePolygon.at(3));
		m_positionType = BottomLeft_Enum;
	}
	else if (grandFatherRect.contains(scenePolygon.at(0))) {
		setPos(scenePolygon.at(0));
		m_positionType = TopLeft_Enum;
	}
	else {
		// 居中显示
		m_positionType = None_Enum;
		setPos(grandFatherRect.center());
	}

}

QFont GraphicsTextItem::getFont() const
{
	QFont font;
	font.setPointSize(12);
	font.setFamily("Poppins-Regular");
	return font;
}

void GraphicsTextItem::paint(QPainter* painter, const QStyleOptionGraphicsItem* option, QWidget* widget)
{
	painter->setRenderHints(QPainter::Antialiasing | QPainter::SmoothPixmapTransform | QPainter::TextAntialiasing);
	QRect textRect = rect().toRect();
	const int raduis = 7;
	painter->setPen(Qt::NoPen);
	painter->setBrush(QColor(44, 125, 250));
	// 绘制小圆点
	painter->drawEllipse(QPointF(0, 0), raduis, raduis);

	// 绘制矩形边框
	textRect.moveTop(-textRect.height() / 2);
	QRect adjustedRect = textRect.adjusted(20, 0, 20, 0);
	if (TopLeft_Enum == m_positionType || BottomLeft_Enum == m_positionType) {
		adjustedRect.moveRight(-raduis * 2);
	}
	painter->drawRoundedRect(adjustedRect, 6, 6);
	// 绘制文字
	painter->setPen(Qt::white);
	QFont font = getFont();
	painter->setFont(font);
	painter->drawText(adjustedRect, Qt::AlignCenter, m_text);
}
